package com.example.springfile.controller;

import com.example.common.util.FileZipUtil;
import com.example.common.util.model.FileNode;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/file")
public class FileController {


    @PostMapping("/zip")
    public void fileZip2(HttpServletResponse response) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        System.out.println("开始执行文件压缩");
        String sourceFile = "/Users/youshang/Downloads/youshang/gc/log/2334/";


        //将N个文件创建为指定的文件目录
        FileNode fileNode1 = new FileNode();
        fileNode1.setDirectoryName("AAAA001");

        FileNode fileNode2 = new FileNode();
        fileNode2.setDirectoryName("AAAA002");
        fileNode2.setFileList(Lists.newArrayList("/Users/youshang/Downloads/youshang/gc/log/file2023/08/11/09/图片素材.pptx"));

        FileNode fileNode3 = new FileNode();
        fileNode3.setDirectoryName("AAAA003");
        fileNode3.setFileList(Lists.newArrayList("/Users/youshang/Downloads/youshang/gc/log/file2023/08/11/09/图片素材.pptx"));

        fileNode1.setFileNode(Lists.newArrayList(fileNode2,fileNode3));

        //将N个文件创建为指定的文件目录
        FileNode fileNode11 = new FileNode();
        fileNode11.setDirectoryName("AAAA001");

        FileNode fileNode22 = new FileNode();
        fileNode22.setDirectoryName("ABBB002");
        fileNode22.setFileList(Lists.newArrayList("/Users/youshang/Downloads/youshang/gc/log/file2023/08/11/09/图片素材.pptx"));

        FileNode fileNode33 = new FileNode();
        fileNode33.setDirectoryName("ABBB003");
        fileNode33.setFileList(Lists.newArrayList("/Users/youshang/Downloads/youshang/gc/log/file2023/08/11/09/图片素材.pptx"));

        FileNode fileNode333 = new FileNode();
        fileNode333.setDirectoryName("ACCC003");
        fileNode333.setFileList(Lists.newArrayList("/Users/youshang/Downloads/youshang/gc/log/file2023/08/11/09/图片素材.pptx"));
        fileNode33.setFileNode(Lists.newArrayList(fileNode333));

        fileNode11.setFileNode(Lists.newArrayList(fileNode22,fileNode33));


        try {
            //将数据转换成指定的文件格式，按文件夹区分
            FileZipUtil.wrapFileDirectory(sourceFile,Lists.newArrayList(fileNode1,fileNode11));
        }catch (Exception e){
            System.out.println("异常信息:"+Throwables.getStackTraceAsString(e));
        }
        stopWatch.stop();
        System.out.println("文件打包成文件夹耗时："+stopWatch.getTotalTimeSeconds());

        stopWatch.start();
        System.out.println("将文件夹打包成压缩包开始");
        //将文件夹全部压缩
        try {
            FileZipUtil.wrapZipFile(response,sourceFile);
        } catch (Exception e) {
//            log.error("压缩文件夹失败，异常信息:{}", Throwables.getStackTraceAsString(e));
            System.out.println("压缩文件夹失败，异常信息:{}"+ Throwables.getStackTraceAsString(e));
        }
        stopWatch.stop();
        System.out.println("将文件夹打包成压缩包结束_耗时:"+stopWatch.getTotalTimeSeconds());

    }
}
